<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/28
 * Time: 16:16
 */

namespace app\admin\controller;

use app\admin\model\UsersModel;
use think\Db;
class Users extends Base
{
    //用户列表
    public function index()
    {
        if(request()->isAjax()){
            $param = input('param.');
            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;
            $where = [];
            if (paramValid($param,'user_id')) $where['user_id'] = $param['user_id'];
            if (paramValid($param,'mobile')) $where['mobile'] = ['like', '%' . $param['mobile'] . '%'];
            if (paramValid($param,'nickname')) $where['nickname'] = ['like', '%' . $param['nickname'] . '%'];
            $users = new UsersModel();
            $selectResult = $users->getUsersByWhere($where, $offset, $limit);
            $vipinfo = ['普通用户','会员','代理','主管','区长','服务商'];
            foreach($selectResult as $key=>$vo){
                $selectResult[$key]['head_pic'] =img($vo['head_pic']);
                $selectResult[$key]['wxqr'] =img($vo['wxqr']);
                // $selectResult[$key]['level_tag'] ='LV'.$vo['level'].' '.level_tag($vo['level']);
                $selectResult[$key]['level_tag'] =$vipinfo[$vo['level']];
                $selectResult[$key]['underling_number'] =Db::name('users')->where(array('first_leader'=>$vo['user_id']))->count();
                $selectResult[$key]['last_login'] = format_date( $vo['last_login']);
                $selectResult[$key]['reg_time'] = format_date( $vo['reg_time']);
                $selectResult[$key]['operate'] = showOperate($this->makeButton($vo['user_id']));
            }
            $return['total'] = $users->getAllUsers($where);  //总数据
            $return['rows'] = $selectResult;

            // return json();
            return json($return);
        }
        $param=input('param.');
        $data=array('user_id'=>0);
        if(isset($param['user_id'])){
            $data['user_id']=$param['user_id'];
        }
        $data['sch']=json_encode(array(
            array('id'=>'user_id','name'=>'会员ID'),
            array('id'=>'mobile','name'=>'手机号'),
            array('id'=>'nickname','name'=>'会员昵称')
        ));
        $this->assign([
            'data' => $data
        ]);
        return $this->fetch();
    }

    //用户举报
    public function complaint(){
        if(request()->isAjax()){
            $param = input('param.');
            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;
            $where = [];
            if (!empty($param['searchText'])) {
                $where['car.car_no'] = ['like', '%' . $param['searchText'] . '%'];
            }
            $users = new UsersModel();
            $selectResult = $users->getComplaint($where, $offset, $limit);
            foreach($selectResult as $key=>$vo){
                $selectResult[$key]['addtime'] =format_date($vo['addtime']);
                $selectResult[$key]['cStatus'] =user_status($vo['cStatus']);
                $selectResult[$key]['operate'] = showOperate(['处理' => [
                    'auth' => 'users/lock',
                    'href' => url('users/lock', ['id' => $vo['complain_user_id']]).'?reurl='.request()->url(),
                    'btnStyle' => 'primary',
                    'icon' => 'fa fa-paste'
                ],]);
            }
            $return['total'] = $users->getAllComplaint($where);  //总数据
            $return['rows'] = $selectResult;
            return json($return);
        }
        $param=input('param.');
        $data=array('user_id'=>0);
        if(isset($param['user_id'])){
            $data['user_id']=$param['user_id'];
        }
        $data['sch']=json_encode(array(
            array('id'=>'searchText','name'=>'车辆编号')
        ));
        $this->assign([
            'data' => $data
        ]);
        return $this->fetch();
    }
    //用户反馈
    public function feedback(){
        if(request()->isAjax()){
            $param = input('param.');
            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;
            $where = [];
            if (!empty($param['searchText'])) {
                $where['content'] = ['like', '%' . $param['searchText'] . '%'];
            }
            $users = new UsersModel();
            $selectResult = $users->getFeedback($where, $offset, $limit);
            foreach($selectResult as $key=>$vo){
                $selectResult[$key]['addtime'] =format_date($vo['addtime']);
            }
            $return['total'] = $users->getAllFeedback($where);  //总数据
            $return['rows'] = $selectResult;
            return json($return);
        }
        $param=input('param.');
        $data=array('user_id'=>0);
        if(isset($param['user_id'])){
            $data['user_id']=$param['user_id'];
        }
        $data['sch']=json_encode(array(
            array('id'=>'searchText','name'=>'反馈内容')
        ));
        $this->assign([
            'data' => $data
        ]);
        return $this->fetch();
    }

    //用户日志
    public function log(){
        if(request()->isAjax()){
            $param = input('param.');
            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;
            $where = [];
            if (!empty($param['searchText'])) {
                $where['log_info'] = ['like', '%' . $param['searchText'] . '%'];
            }
            $users = new UsersModel();
            $selectResult = $users->getLog($where, $offset, $limit);
            foreach($selectResult as $key=>$vo){
                $selectResult[$key]['log_time'] =format_date($vo['log_time']);
            }
            $return['total'] = $users->getAllLog($where);  //总数据
            $return['rows'] = $selectResult;
            return json($return);
        }
        $param=input('param.');
        $data=array('user_id'=>0);
        if(isset($param['user_id'])){
            $data['user_id']=$param['user_id'];
        }
        $data['sch']=json_encode(array(
            array('id'=>'searchText','name'=>'日志描述')
        ));
        $this->assign([
            'data' => $data
        ]);
        return $this->fetch();
    }
    // 添加用户
    /*
    public function userAdd()
    {
        if(request()->isPost()){

            $param = input('post.');

            $param['password'] = md5($param['password']);
            $user = new UsersModel();
            $flag = $user->insertUser($param);

            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }

        return $this->fetch();
    }
*/
    public function detail(){
        $id = input('param.id');
        $user = new UsersModel();
        $userinfo = $user->getDetail($id);
        $vipinfo = ['普通用户','会员','代理','主管','区长','服务商'];
        $userinfo['level_tag'] = $vipinfo[$userinfo['level']];
        $this->assign([
            'user' => $userinfo
        ]);
        return $this->fetch();
    }
    // 编辑用户
    public function edit()
    {
        $user = new UsersModel();

        if(request()->isPost()){

            $param = input('post.');

            if(empty($param['password'])){
                unset($param['password']);
            }else{
                $param['password'] = md5($param['password']);
            }
            if(isset($param['status']) && $param['status']>1){
                $param['status_time']=time();
            }
            $flag = $user->editUser($param);
            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }

        $id = input('param.id');

        $this->assign([
            'user' => $user->getOneUser($id),
            'status' => config('user_status'),
        ]);
        return $this->fetch();
    }
    // 处理用户
    public function lock()
    {
        $user = new UsersModel();

        if(request()->isPost()){

            $param = input('post.');
            //dump($param);exit;
            if(isset($param['status']) && $param['status']>1){
                $param['status_time']=time();
            }
            $flag = $user->editUser($param);
            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }

        $id = input('param.id');
        $param=input('param.');
        $reurl='';
        if(isset($param['reurl'] )) $reurl=input('param.reurl');
        $this->assign([
            'user' => $user->getOneUser($id),
            'reurl' => $reurl,
            'status' => config('user_status'),
        ]);
        return $this->fetch();
    }
    // 删除用户
    public function del()
    {
        $id = input('param.id');

        $user = new UsersModel();
        $flag = $user->delUser($id);
        return json(msg($flag['code'], $flag['data'], $flag['msg']));
    }
    /**
     * 拼装操作按钮
     * @param $id
     * @return array
     */

    private function makeButton($id)
    {
        return [
            '编辑' => [
                'auth' => 'users/edit',
                'href' => url('users/edit', ['id' => $id]),
                'btnStyle' => 'primary',
                'icon' => 'fa fa-paste'
            ],
            '详情' => [
                'auth' => 'users/detail',
                'href' => "javascript:detail('" .$id ."')",
                'btnStyle' => 'primary',
                'icon' => 'fa fa-eye'
            ]
            /*
            '删除' => [
                'auth' => 'users/del',
                'href' => "javascript:userDel(" .$id .")",
                //'href' => url('users/userEdit', ['id' => $id]),
                'btnStyle' => 'danger',
                'icon' => 'fa fa-trash-o'
            ]
            */
        ];
    }
}